//
//  ThemeController.m
//  SmallEasyLife
//
//  Created by qianfeng on 15/10/24.
//  Copyright © 2015年 GXK. All rights reserved.
//

#import "ThemeController.h"

#import "Masonry.h"

@interface ThemeController ()
@property (weak, nonatomic) IBOutlet UIView *animationView;
@property (weak, nonatomic) IBOutlet UIButton *changeButton;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) UIView * listView;
@property (nonatomic) NSInteger status;

@end

@implementation ThemeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.listView.backgroundColor = [UIColor redColor];
    
    if (self.webViewUrl)
    {
        
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webViewUrl]]];
    }
}

- (IBAction)shareBarItemTaped:(UIBarButtonItem *)sender {
    NSLog(@"Share!!!");
}

- (IBAction)changeButtonClicked:(UIButton *)sender {
    
    
    
    if (self.status == 0)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.animationView cache:YES];
        [UIView commitAnimations];
        [self.animationView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        self.status = 1;
        [sender setImage:[UIImage imageNamed:@"themeweb"] forState:UIControlStateNormal];
    }
    else if(self.status == 1)
    {

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.animationView cache:YES];
        [self.animationView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        [UIView commitAnimations];
        [sender setImage:[UIImage imageNamed:@"themelist"] forState:UIControlStateNormal];
        self.status= 0;
    }
    
    
    
}

- (void)setWebViewUrl:(NSString *)webViewUrl
{
    if (_webViewUrl != webViewUrl) {
        _webViewUrl = webViewUrl;
        
//        NSLog(@"webViewUrl in themeController -> %@",webViewUrl);
        //这里view尚未didload，请求是同步的，所以导致页面加载出来是空白的
//        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webViewUrl]]];
    }
    
}

- (UIView *)listView
{
    if (_listView == nil) {
        UIView * listView = [[UIView alloc] initWithFrame:CGRectZero];
        _listView = listView;
        listView.backgroundColor = [UIColor redColor];
        [self.animationView insertSubview:listView belowSubview:self.webView];
        
        [listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.animationView.mas_top);
            make.left.equalTo(self.animationView.mas_left);
            make.width.equalTo(self.webView.mas_width);
            make.height.equalTo(self.webView.mas_height);
        }];
    }
    return _listView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
